package com.rome.hotel.data.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.rome.hotel.common.annotation.Anonymous;
import com.rome.hotel.data.domain.HotelOrder;
import com.rome.hotel.data.service.IHotelOrderService;
import io.jsonwebtoken.lang.Assert;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rome.hotel.common.annotation.Log;
import com.rome.hotel.common.core.controller.BaseController;
import com.rome.hotel.common.core.domain.AjaxResult;
import com.rome.hotel.common.enums.BusinessType;
import com.rome.hotel.data.domain.HotelReserve;
import com.rome.hotel.data.service.IHotelReserveService;
import com.rome.hotel.common.utils.poi.ExcelUtil;
import com.rome.hotel.common.core.page.TableDataInfo;

/**
 * 预定Controller
 * 
 * @author rome
 * @date 2024-01-20
 */
@RestController
@Anonymous
@RequestMapping("/data/reserve")
public class HotelReserveController extends BaseController
{
    @Autowired
    private IHotelReserveService hotelReserveService;
    
    @Autowired
    private IHotelOrderService  hotelOrderService;

    /**
     * 查询预定列表
     */
//    @PreAuthorize("@ss.hasPermi('data:reserve:list')")
    @GetMapping("/list")
    public TableDataInfo list(HotelReserve hotelReserve)
    {
        startPage();
        List<HotelReserve> list = hotelReserveService.selectHotelReserveList(hotelReserve);
        return getDataTable(list);
    }

    /**
     * 导出预定列表
     */
    @PreAuthorize("@ss.hasPermi('data:reserve:export')")
    @Log(title = "预定", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HotelReserve hotelReserve)
    {
        List<HotelReserve> list = hotelReserveService.selectHotelReserveList(hotelReserve);
        ExcelUtil<HotelReserve> util = new ExcelUtil<HotelReserve>(HotelReserve.class);
        util.exportExcel(response, list, "预定数据");
    }

    /**
     * 获取预定详细信息
     */
    @PreAuthorize("@ss.hasPermi('data:reserve:query')")
    @GetMapping(value = "/{reserveId}")
    public AjaxResult getInfo(@PathVariable("reserveId") Long reserveId)
    {
        return AjaxResult.success(hotelReserveService.selectHotelReserveByReserveId(reserveId));
    }

    /**
     * 新增预定
     */
//    @PreAuthorize("@ss.hasPermi('data:reserve:add')")
    @Log(title = "预定", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HotelReserve hotelReserve)
    {
        String orderNumber = hotelReserveService.insertHotelReserve(hotelReserve);
        return AjaxResult.success(orderNumber);
//        return toAjax(hotelReserveService.insertHotelReserve(hotelReserve));
    }

    /**
     * 修改预定
     */
//    @PreAuthorize("@ss.hasPermi('data:reserve:edit')")
    @Log(title = "预定", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HotelReserve hotelReserve)
    {
        Assert.state( hotelReserveService.updateHotelReserve(hotelReserve)!=0,"这个时间已被预约");
        return AjaxResult.success();
    }

    /**
     * 删除预定
     */
//    @PreAuthorize("@ss.hasPermi('data:reserve:remove')")
    @Log(title = "预定", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reserveIds}")
    public AjaxResult remove(@PathVariable Long[] reserveIds)
    {
        try {
            return toAjax( hotelReserveService.deleteHotelReserveByReserveIds(reserveIds));
        }catch (Exception e){
            return  AjaxResult.error("数据异常，请联系管理员");
        }
    }

    /**
     * 查询在预约时间段的
     */
//    @PreAuthorize("@ss.hasPermi('data:reserve:listQueryTime')")
    @GetMapping("/listQueryTime")
    public AjaxResult listQueryTime(@Param("roomNumber") String roomNumber)
    {
        HotelReserve hotelReserve = new HotelReserve();
        hotelReserve.setRoomNumber(roomNumber);
        Date time = new Date();
        Calendar calendar   =   new GregorianCalendar();
        calendar.setTime(time);
        calendar.add(calendar.DATE,-1);//把日期往后增加一天.整数往后推,负数往前移动
        time=calendar.getTime();   //这个时间就是日期往后推一天的结果
        hotelReserve.setCheckOutTime(time);
        List<HotelReserve> list = hotelReserveService.selectHotelReserveQueryByCheckOut(hotelReserve);
        return AjaxResult.success(list);
    }

    /**
     * 根据入住时间查
     */
    @PreAuthorize("@ss.hasPermi('data:reserve:listQueryTimeByJoinTime')")
    @GetMapping("/listQueryTimeByJoinTime")
    public AjaxResult listQueryTimeByJoinTime(HotelReserve hotelReserve)
    {
        List<HotelReserve> list = hotelReserveService.selectHotelReserveByJoinTime(hotelReserve);
        return AjaxResult.success(list);
    }

    /**
     * 查询预定列表根据账号查
     */
//    @PreAuthorize("@ss.hasPermi('data:reserve:list')")
    @GetMapping("/listReserveByUserName")
    public TableDataInfo listReserveByUserName(String userName,
                                               Integer pageSize,
                                              Integer pageNum
                                               )
    {
//        1.通过前端传送的账号，去订单表查询
        HotelOrder hotelOrder = new HotelOrder();
        hotelOrder.setUserName(userName);
        List<HotelOrder> hotelOrders = hotelOrderService.selectHotelOrderList(hotelOrder);
        Assert.state(hotelOrders.size()>0,"没有一条预约");
       List<String> orderNumbers = new ArrayList<String>();
        for (HotelOrder item:hotelOrders
             ) {
            orderNumbers.add(item.getOrderId());
        }

        String[] array = orderNumbers.stream().toArray(String[]::new);

//        startPage();
        PageHelper.startPage(pageNum, pageSize);
        System.out.println(PageHelper.getLocalPage());
        List<HotelReserve> list = hotelReserveService.selectHotelReserveByorderNumbers(array);
        return getDataTable(list);
    }
}
